﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharpAlgorithm.Base;

namespace CSharpAlgorithm
{
    class Allergy
    {
        public Allergy()
        {
            int testCase = Int32.Parse(TestConsole.ReadLine());
            while (testCase-- > 0)
            {
                string[] input = TestConsole.ReadLine().Split(' ');
                int friendCount = Int32.Parse(input[0]);
                int foodCount = Int32.Parse(input[1]);

                List<Edge>[] G = new List<Edge>[friendCount];
                int edgeCount = 0;
                for (int i = 0; i < friendCount; i++)
                {
                    G[i] = new List<Edge>();
                }
                for (int i = 0; i < foodCount; i++)
                {
                    input = TestConsole.ReadLine().Split(' ');
                    int sameCount = Int32.Parse(input[0]) - 1;
                    int from = Int32.Parse(input[1]);
                    
                    for (int j = 0; j < sameCount; j++)
                    {
                        int to = Int32.Parse(input[j + 2]);
                        G[from].Add(new Edge(from, to, 0, 0, 0));
                        edgeCount++;
                    }
                }
                BipartiteMatching bi = new BipartiteMatching(G, edgeCount, friendCount);
                Console.WriteLine(bi.Matching());
            }
        }
    }
}
